package top.yangbuyi.controller.system;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.yangbuyi.acpect.annotaction.Log;
import top.yangbuyi.acpect.enums.BusinessType;
import top.yangbuyi.dto.UserDto;
import top.yangbuyi.service.impl.UserService;
import top.yangbuyi.utils.ShiroSecurityUtils;
import top.yangbuyi.vo.AjaxResult;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


/**
 * @description: 杨不易网站:www.yangbuyi.top
 * @program: yangbuyiopenHis2020
 * @ClassName: UserController
 * @create: 2020-08-29 19:45
 * @author: yangbuyi
 * @since： JDK1.8
 * @UserController: 用户控制器$ 用户控制器
 **/

@RestController
@Slf4j
@RequestMapping("system/user")
public class UserController {

	  @Autowired
	  private UserService userService;

	  /**
	   * @功能描述: 用户分页查询
	   * @Author: TeouBle
	   * @Date: 2020/9/8 0：0：0
	   */
	  @GetMapping("listUserForPage")
	  public AjaxResult listUserForPage(UserDto userDto) {
			return AjaxResult.success(userService.listUserForPage(userDto));
	  }

	  /**
	   * @功能描述:查询所有可用的用户
	   * @Author: TeouBle
	   * @Date: 2020/9/8 0：0：0
	   */
	  @GetMapping("selectAllUser")
	  public AjaxResult selectAllUser() {
			return AjaxResult.success(this.userService.getAllUsers());
	  }

	  /**
	   * @功能描述: 用户添加
	   * @Author: TeouBle
	   * @Date: 2020/9/8 0：0：0
	   */
	  @PostMapping("addUser")
	  @Log(title = "用户添加", businessType = BusinessType.INSERT)
	  public AjaxResult addUser(@Validated UserDto userDto) {
			// 设置添加人员
			userDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
			return AjaxResult.toAjax(this.userService.addUser(userDto));
	  }

	  /**
	   * @功能描述: 用户修改
	   * @Author: TeouBle
	   * @Date: 2020/9/8 0：0：0
	   */
	  @PutMapping("updateUser")
	  @Log(title = "用户修改", businessType = BusinessType.UPDATE)
	  public AjaxResult updateUser(@Validated UserDto userDto) {
			try {
				  // 设置修改人员
				  userDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
				  return AjaxResult.toAjax(this.userService.updateUser(userDto));
			} catch (Exception e) {
				  return AjaxResult.ex(e.getMessage());
			}
	  }

	  /**
	   * @功能描述: 根据ID查询一个用户信息
	   * @Author: TeouBle
	   * @Date: 2020/9/8 0：0：0
	   */
	  @GetMapping("getUserById/{useridid}")
	  @Log(title = "根据ID查询一个用户信息", businessType = BusinessType.SELECT)
	  public AjaxResult getUserByID(@NotNull(message = "用户ID不能为空") @PathVariable @Validated Long useridid) {
			try {
				  return AjaxResult.success(this.userService.getOne(useridid));
			} catch (Exception e) {
				  return AjaxResult.ex(e.getMessage());
			}
	  }

	  /**
	   * @功能描述:删除
	   * @Author: TeouBle
	   * @Date: 2020/9/8 0：0：0
	   */
	  @DeleteMapping("deleteUserByIds/{useridIds}")
	  @Log(title = "根据ID删除用户", businessType = BusinessType.DELETE)
	  public AjaxResult updateUser(@PathVariable @Validated @NotEmpty(message = "要删除的用户ID不能为空") Long[] useridIds) {
			try {
				  return AjaxResult.toAjax(this.userService.deleteUserByIds(useridIds));
			} catch (Exception e) {
				  return AjaxResult.ex(e.getMessage());
			}
	  }

	  /**
	   * 重置密码
	   */
	  @PostMapping("resetPwd/{userIds}")
	  public AjaxResult resetPwd(@PathVariable Long[] userIds) {
			if (userIds.length > 0) {
				  this.userService.resetPassWord(userIds);
				  return AjaxResult.success("重置成功");
			}
			return AjaxResult.fail("重置失败,没有选择用户");

	  }

}
